﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Model
{
    public class MessageClipRepository
    {
        private JzSportDbContext _dbContext;

        public MessageClipRepository(JzSportDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public void Create(MessageClip messageClip)
        {
            _dbContext.Set<MessageClip>().Add(messageClip);
        }

        public MessageClip Get(Guid id)
        {
            return _dbContext.Set<MessageClip>().Where(p => p.Id == id).SingleOrDefault();
        }

        public MessageClip Get(int clipType, string clipIdentityText)
        {
            return _dbContext.Set<MessageClip>().Where(p => p.Type == clipType && p.IdentityText == clipIdentityText).SingleOrDefault();
        }

        public void Update(MessageClip messageClip)
        {
            _dbContext.Entry<MessageClip>(messageClip).State = System.Data.EntityState.Modified;
        }

        public void Delete(MessageClip messageClip)
        {
            _dbContext.Entry<MessageClip>(messageClip).State = System.Data.EntityState.Deleted;
        }

        public List<MessageClip> GetList(int userId, int page, int limit)
        {
            return _dbContext.Set<MessageClip>().Where(p => p.ReceiverId == userId && p.IsDeleted == false)
                .OrderBy(p => p.LastestUpdatedTime)
                .Take(limit)
                .Skip(page * limit)
                .ToList();
        }

        public List<Data.MessageClipView> GetListByType(int userId, int type, int start, int limit)
        {
            return _dbContext.Database.SqlQuery<Data.MessageClipView>("exec [SP_GetMessageClip] @userid={0}, @type={1}, @start={2},@limit={3}",
                new object[] { userId, type, start, limit }).ToList();
        }

        public Data.NewMessageCount GetNewMessageCount(int userId)
        {
            return _dbContext.Database.SqlQuery<Data.NewMessageCount>("exec [SP_GetNewMessageCount] @userid={0}",
                new object[] { userId }).SingleOrDefault();
        }
    }
}
